#include "CameraImage.h"
#include <QDebug>
CameraImage::CameraImage(QObject *parent)
    : QAbstractVideoSurface(parent),
    m_imageCount(0),
    m_flipMode(NONE),
    m_bayerPattern(COLOR_FILTER_GBRG),
    m_bayerMethod(BAYER_METHOD_BILINEAR)
{

}

QList<QVideoFrame::PixelFormat> CameraImage::supportedPixelFormats(
        QAbstractVideoBuffer::HandleType handleType) const
{
    Q_UNUSED(handleType);
    QList<QVideoFrame::PixelFormat> list;
    list << QVideoFrame::Format_RGB32;
    list << QVideoFrame::Format_ARGB32;
    list << QVideoFrame::Format_RGB24;
    list << QVideoFrame::Format_UYVY;
    list << QVideoFrame::Format_Y8;
    list << QVideoFrame::Format_YUYV;
    return list;
}

bool CameraImage::present(const QVideoFrame &frame)
 {
    // 处理捕获的帧
    if(frame.isMapped())
    {
        setVideoFrame(frame);
    }
    else
    {
        QVideoFrame f = frame;
        f.map(QAbstractVideoBuffer::ReadOnly);
        setVideoFrame(f);
    }
    return true;
 }

void CameraImage::setVideoFrame(const QVideoFrame &frame)
{
//    qDebug() << "setVideoFrame";
//    qDebug() << "height = " << frame.height();
//    qDebug() << "width = " << frame.width();
//    qDebug() << frame.pixelFormat();
    switch (frame.pixelFormat())
    {
    case QVideoFrame::Format_RGB24:
        setRGB24Image(frame.bits(), frame.size());
        break;
    case QVideoFrame::Format_RGB32:
    case QVideoFrame::Format_ARGB32:
    case QVideoFrame::Format_ARGB32_Premultiplied:
        setRGB32Image(frame.bits(), frame.size());
        break;
    case QVideoFrame::Format_Y8:
        setBayerImage(frame.bits(), frame.size());
        break;
    case QVideoFrame::Format_YUYV:
        setYUY2Image(frame.bits(), frame.size());
        break;
    case QVideoFrame::Format_UYVY:
        setUYVYImage(frame.bits(), frame.size());
        break;
    default:
        break;
    }
}

void CameraImage::setFlipMode(enum FlipMode flipMode)
{
    if(flipMode == m_flipMode)
    {
        return;
    }
    m_flipMode = flipMode;
    emit flipModeChanged(flipMode);
}

void CameraImage::setHorizontalFlip(bool on)
{
    qDebug() << "setHorizontalFlip = " << on;
    int flip_mode;
    on ? flip_mode = m_flipMode | 0x01: flip_mode = m_flipMode & 0x02;
    setFlipMode(static_cast<FlipMode> (flip_mode));
}

void CameraImage::setVerticalFlip(bool on)
{
    qDebug() << "setVerticalFlip = " << on;
    int flip_mode;
    on ? flip_mode = m_flipMode | 0x02: flip_mode = m_flipMode & 0x01;
    setFlipMode(static_cast<FlipMode> (flip_mode));
}

void CameraImage::setBayerPattern(BAYER_PATTERN pattern)
{
    pattern = qBound(COLOR_FILTER_RGGB, pattern, COLOR_FILTER_BGGR);
    if(pattern == m_bayerPattern)
    {
        return;
    }
    m_bayerPattern = pattern;
    qDebug() << "m_bayer_pattern = " << m_bayerPattern;
    emit bayerPatternChanged(pattern);

}

void CameraImage::setBayerMethod(BAYER_METHOD method)
{
    method = qBound(BAYER_METHOD_NONE, method, BAYER_METHOD_EDGESENSE);
    if(method == m_bayerMethod)
    {
        return;
    }
    m_bayerMethod = method;
    qDebug() << "m_bayer_method = " << m_bayerMethod;
    emit bayerMethodChanged(method);
}

bool CameraImage::loadImage(const QString &fileName)
{
    qDebug() << "loadImage";
    QImage image;
    image.load(fileName);

    if(image.isNull())
    {
        qDebug() << "image.isNull() == true";
        return false;
    }
    setImage(image);
    return true;
}

bool CameraImage::saveImage( const QString &fileName )
{
    m_imageMutex.lock();
    QImage image = m_image.copy();
    m_imageMutex.unlock();
    return image.save(fileName);
}


QImage CameraImage::getImage()
{
    QImage image;
    // 防止在获取图像时有其他的线程更新图像，这里要加锁
    m_imageMutex.lock();
    image = m_image.copy();
    m_imageMutex.unlock();
    return image;
}
